package com.example.lottery.controller;

import com.example.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.example.lottery.common.exception.ControllerException;
import com.example.lottery.common.pojo.CommonResult;
import com.example.lottery.common.utils.JacksonUtil;
import com.example.lottery.controller.param.CreatePrizeParam;
import com.example.lottery.controller.param.PageParam;
import com.example.lottery.controller.result.FindPrizeListResult;
import com.example.lottery.service.PictureService;
import com.example.lottery.service.PrizeService;
import com.example.lottery.service.dto.PageListDTO;
import com.example.lottery.service.dto.PrizeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;


@RestController
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    private PrizeService prizeService;

    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);

    @Autowired
    private PictureService pictureService;

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String upload(MultipartFile file) {
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     * @param param
     * @param picFile
     * @return
     */
    //RequestPart：接收表单数据 multipart/form-data  用于上传文件 或者是上传文件+用户信息
    @RequestMapping("/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic")MultipartFile picFile) {
        return CommonResult.success(prizeService.createPrize(param,picFile));
    }

    /**
     * 获取奖励列表
     * @param param
     * @return
     */
    @RequestMapping("/find-list")
    public CommonResult<FindPrizeListResult> findprizeList(PageParam param) {
        logger.info("findprizeList param:{}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindprizeListResult(pageListDTO));
    }


    /**
     * 将pageListDTO转换成VO
     * @param pageListDTO
     * @return
     */
    private FindPrizeListResult convertToFindprizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (pageListDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }

        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO
                        .getRecords()
                        .stream()
                        .map(prizeDTO -> {
            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
            prizeInfo.setPrice(prizeDTO.getPrice());
            prizeInfo.setPrizeName(prizeDTO.getName());
            prizeInfo.setDescription(prizeDTO.getDescription());
            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
            return prizeInfo;
        }).collect(Collectors.toList()));
        return result;
    }
}
